package com.rupertjones.globalcron.web.munin;

import java.util.ArrayList;
import java.util.List;

import static java.lang.String.format;

/**
 * <p>&copy Rupert Jones 2012</p>
 *
 * @author rup
 */
public class MuninGraphConfig {

    private String title = null;
    private String verticalLabel = null;
    private String category = null;
    private String total = null;
    private List<MuninGraphValueConfig> valueConfigurationItems = new ArrayList<MuninGraphValueConfig>();

    public void addValueConfig(MuninGraphValueConfig valueConfig) {
        valueConfigurationItems.add(valueConfig);
    }

    public String getTitle() {
        return title;
    }

    public boolean hasTitle() {
        return !isEmpty(getTitle());
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getVerticalLabel() {
        return verticalLabel;
    }

    public boolean hasVerticalLabel() {
        return !isEmpty(getVerticalLabel());
    }

    public void setVerticalLabel(String verticalLabel) {
        this.verticalLabel = verticalLabel;
    }

    public boolean hasCategory() {
        return !isEmpty(getCategory());
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getTotal() {
        return total;
    }

    public boolean hasTotal() {
        return !isEmpty(getTotal());
    }

    public void setTotal(String total) {
        this.total = total;
    }

    public String output() {
        StringBuilder builder = new StringBuilder();
        if (hasTitle()) {
            builder.append(format("graph_title %s\n", getTitle()));
        }

        if (hasVerticalLabel()) {
            builder.append(format("graph_vlabel %s\n", getVerticalLabel()));
        }

        if (hasCategory()) {
            builder.append(format("graph_category %s\n", getCategory()));
        }

        if (hasTotal()) {
            builder.append(format("graph_total %s\n", getTotal()));
        }

        for (MuninGraphValueConfig thisConfig : valueConfigurationItems) {
            builder.append(format("%s\n", thisConfig.output()));
        }

        return builder.toString();
    }

    private boolean isEmpty(String title) {
        if (title == null || title.trim().isEmpty()) {
            return true;
        }
        return false;
    }
}
